<?php
namespace Amoysea\Controller;
use Think\Controller;
class ImportController extends BaseController {
	public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
        $db_name=$g['db'];
        $this->product = M('product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
    }
	public function importListproduct(){
        $this->display();
    }
	//导入采购清单
	function doimportListproduct(){
		$g=session('company');
        $db_name=$g['db'];
		$trans=M('', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$trans->startTrans();   // 开启事务
		$m=M('ListProduct',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		
		$list_id=I('id');
		if (!empty($_FILES)){
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',    
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);   
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数

			for($i=2;$i<=$highestRow;$i++)
			{
				$data['product_id']		= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$data['warehouse_code']		= (string)$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['warehouse_name']		= (string)$objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['price']		= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['count']		= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['unit_no']		= (string)$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['unit_name']	= (string)$objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data['expected_time']			= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data['unit_t_no']		= (string)$objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data['unit_t_count']			= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$data['unit_t_name']		= (string)$objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				$data['other_count']		= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
				
				$data['list_id']=$list_id;
				$data['is_shelves']=0;
				$data['is_back']=0;
				$data['is_delete']=0;
				$data['is_push']=0;
				$massage=session('company');
				$data['operator']=$massage['username'];
				$data['created_at']=date("Y-m-d H:i:s",time());
				$data['is_examined']=0;
				//添加
				$res=$m->add($data);
				if(!$res){
					$trans->rollback();
				}
			}
			$trans->commit();
			$return['status']=1;
			$return['message']='导入成功！';
			$this->ajaxReturn($return);
		}else
		{
			$return['status']=0;
			$return['message']='请选择上传的文件';
			$this->ajaxReturn($return);
		}
	}
	public function importProduct(){
        $this->display();
    }
	public function importPushorder(){
        $this->display();
    }
	//导入产品
	function doimportProduct(){
		$g=session('company');
        $db_name=$g['db'];
		$trans=M('', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$trans->startTrans();   // 开启事务
		$m=M('Product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		$b=M('Book',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		
		if (!empty($_FILES)){
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',    
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息 
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			
			for($i=2;$i<=$highestRow;$i++)
			{
				$data['name']		= (string)$objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$data['unit']		= (string)$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['unit_no']		= (string)$objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['standard']		= (string)$objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['unit_t']		= (string)$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['unit_no_t']		= (string)$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['unit_t_goods_spec']	= (string)$objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data['sn']		= (string)$objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				
		
				$data['pn']			= (string)$objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data['coin_code']		= (string)$objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$data['currency']		= (string)$objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				$data['country']		= (string)$objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
				$data['country_code']		= (string)$objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
				$data['zccode']		= (string)$objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
				$data['category']		= (string)$objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();
				$data['item_no']		= (string)$objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
				$data['item_type']		= (string)$objPHPExcel->getActiveSheet()->getCell("Q".$i)->getValue();
				$data['hs_code']		= (string)$objPHPExcel->getActiveSheet()->getCell("R".$i)->getValue();
				
				$data['created_at']=date("Y-m-d H:i:s",time());
				$data['is_examined']=0;				
				
				$info=$b->where(array('book_no'=>$data['zccode']))->find();
				if(empty($info)){
					$trans->rollback();
					$return['status']=0;
					$return['message']='没有查到对应账册信息，请先添加对应账册信息或核对数据后再导入！';
					$this->ajaxReturn($return);
				}
				
				$merchant_code=$info['merchant_code'];
				$sn_length=strlen($sn);
				// if($sn_length<20){
					// $sn_new=$sn;
					// for($i=0;$i<20-$sn_length;$i++){
						// $sn_new=$sn_new.'0';
					// }
				// }else{
					// $sn_new=substr($sn,-20,20);
				// }
				$sn_new=$data['sn'];
				$data['goods_no']=$merchant_code.$sn_new;//商家编码（10位）+ 条形码（不足20位数字的在后面补0，超过20位的，截取后面的20位字符）
				//商家编码定义：公司--C+海关十位编码或者组织机构代码。如果有海关十位编码的优先考虑海关十位编码，没有海关十位编码的再考虑组织机构代码
				//注：组织机构代码不足十位的在后边补0
				//个人--P+手机号码。截取手机号码后十位
				//添加
				$res=$m->add($data);	
				// echo $m->getlastsql();
				if(!$res){
					$trans->rollback();
				}
			}
			$trans->commit();
			$return['status']=1;
			$return['message']='导入成功！';
			$this->ajaxReturn($return);
		}else
		{
			$return['status']=0;
			$return['message']='请选择上传的文件';
			$this->ajaxReturn($return);
		}
	}
	
	//导入订单
	function doimportPushorder(){
		$g=session('company');
        $db_name=$g['db'];
		$trans = M('',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $trans->startTrans();   // 开启事务
		$m=M('Pushorder',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		$g=M('Pushordergoods',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		$p=M('Printorder',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		
		if (!empty($_FILES)) {
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息      
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			// $sql = 'truncate table '.$db_name.'.'.C('DB_PREFIX_T').'arrival_order';
			// $stat=M()->execute($sql);//清空表
			
			for($i=2;$i<=$highestRow;$i++)
			{
				$outbound=A('Outbound');
				//流水订单号
				$serialNumber=$outbound->serialNumber('Pushorder');
				$serialNumber='P'.date("Ymd",time()).$serialNumber;
				//流水订单号
				$order_no		= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();//订单编号
				$data['order_no']=$serialNumber;
				if($order_no!=$old_order_no){//不重复
					//重复判断--beging
					$m_info=$m->where(array('order_no'=>$order_no))->find();
					if(!empty($m_info)){
						$return['status']=1;
						$return['message']="要导入的订单编号已经存在！";
						$this->ajaxReturn($return);
					}
					//重复判断-end
				}
				$data['user_name']		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['user_phone']		= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['user_idcard']	= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['province']		= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				
				$data['city']			= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['area']			= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data['user_address']	= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();

				$data['total_price']	= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();//总价
				$data['sendMan']	= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();//发货人
				$data['sendManPhone']=$objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();//发货人电话
				$data['sendManAddress']=$objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();//发货人地址
				if($data['sendManAddress'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $data['sendManAddress'] = $data['sendManAddress']->__toString();
                }
				$data['sendProvince']=$objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();//发货人省
				$data['sendCity']=$objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();//发货人市
				$data['sendCounty']=$objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();//发货人区/县
				
				$data['pay_status']	= $objPHPExcel->getActiveSheet()->getCell("T".$i)->getValue();
				$data['flag']	= $objPHPExcel->getActiveSheet()->getCell("U".$i)->getValue();
				$data['order_cate2']	= $objPHPExcel->getActiveSheet()->getCell("V".$i)->getValue();
				
				$data['add_time']		= time();

				$data_og['goods_no']	= $objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
				$data_og['goods_name']	= $objPHPExcel->getActiveSheet()->getCell("Q".$i)->getValue();
				$data_og['goods_nums']	= $objPHPExcel->getActiveSheet()->getCell("R".$i)->getValue();
				$data_og['goods_price']	= $objPHPExcel->getActiveSheet()->getCell("S".$i)->getValue();
				
				$print_order['receiveMan']=$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$print_order['receiveManPhone']=$objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$print_order['receiveProvince']=$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$print_order['receiveCity']=$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$print_order['receiveCounty']=$objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$print_order['receiveManAddress']=$objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				if($print_order['receiveManAddress'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $print_order['receiveManAddress'] = $print_order['receiveManAddress']->__toString();
                }
				
				$print_order['sendMan']=$objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();//发货人
				$print_order['sendManPhone']=$objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();//发货人电话
				$print_order['sendManAddress']=$objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();//发货人地址
				if($print_order['receiveManAddress'] instanceof \PHPExcel_RichText) { //富文本转换字符串
                    $print_order['receiveManAddress'] = $print_order['receiveManAddress']->__toString();
                }
				$print_order['sendProvince']=$objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();//发货人省
				$print_order['sendCity']=$objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();//发货人市
				$print_order['sendCounty']=$objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();//发货人区/县
				$print_order['txLogisticID']=$objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$print_order['order_no']=$serialNumber;
				
				$print_order['add_time']=time();//添加时间
				
				if($order_no!=$old_order_no){//不重复
					//添加订单
					$insert_id=$m->add($data);
					if(!$insert_id){
						$trans->rollback();
					}
					//添加订单详情
					$data_og['order_id']=$insert_id;
					$res_g=$g->add($data_og);
					if(!$res_g){
						$trans->rollback();
					}
					//添加面单
					$res_p=$p->add($print_order);
					if(!$res_p){
						$trans->rollback();
					}
					$order=A('Order');
					$res_edit=$order->edit_storagegoodsnums($data_og['goods_no'],$data_og['goods_nums'],false);//扣库存
					if(!$res_edit){
						$trans->rollback();
					}
					//扣订单金额
					$childs=session('company.childs');
					if(!empty($childs)){
						$username=$childs['username'];
						$res_outfee=$order->outfee($username,$data_og['goods_price']*$data_og['goods_nums']);
						if(!$res_outfee){
							$trans->rollback();
						}
					}
					//添加对账单
					$checkbill=A('Checkbill');
					$res_checkbill=$checkbill->confirm_adds($data_og['goods_price']*$data_og['goods_nums'],$data['order_no']);
					if(!$res_checkbill){
						$trans->rollback();
					}
				}else{//重复
					//添加订单详情
					$data_og['order_id']=$insert_id;
					$res_gg=$g->add($data_og);
					if(!$res_gg){
						$trans->rollback();
					}
				}
				$old_order_no=$order_no;
			}
			$trans->commit();
			$return['status']=1;
			$return['message']="导入成功！";
			$this->ajaxReturn($return);
		}else
		{
			$return['status']=0;
			$return['message']="请选择上传的文件!";
			$this->ajaxReturn($return);
		}
	}
	public function importgoods(){
        $this->display();
    }
	public function importstoragegoods(){
		$warehouse_code=I('warehouse_code');
		$this->assign('warehouse_code',$warehouse_code);
        $this->display();
    }
	//导入产品
	function doimportstorageGoods(){
		if (!empty($_FILES)){
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',    
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息 
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			
			for($i=2;$i<=$highestRow;$i++)
			{
				$data[$i-2]['order_no']		= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$data[$i-2]['batch_no']		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data[$i-2]['manual_id']		= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data[$i-2]['goods_no']		= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data[$i-2]['goods_name']		= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				
				$data[$i-2]['goods_spec']		= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data[$i-2]['unit1']			= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data[$i-2]['unit2']			= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data[$i-2]['storage_amount']	= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				// $data[$i-2]['declare_amount']	= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$data[$i-2]['receive_amount']	= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data[$i-2]['price']			= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$data[$i-2]['total_price']	= $data[$i-2]['price']*$data[$i-2]['storage_amount'];
				
				$data[$i-2]['country_code']	= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				$data[$i-2]['currency']		= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
				$data[$i-2]['bar_code']		= $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
				$data[$i-2]['conforming_p']	= $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
				$data[$i-2]['nonconforming_p']= $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();
				$data[$i-2]['scrap_one']		= $objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
				$data[$i-2]['overdue_time']	= $objPHPExcel->getActiveSheet()->getCell("Q".$i)->getValue();
				$data[$i-2]['erp_code']	= $objPHPExcel->getActiveSheet()->getCell("R".$i)->getValue();
			}
			$data_push['warehouse_code']=I('warehouse_code');
			$data_push['rows']=$data;
			$url=C('base_config.importArrivalorder');
			// dump($data_push);
			$res=push($url,$data_push);
			// dump($res);
			$res=json_decode($res,true);
			if($res['status']){
				$return['status']=1;
				$return['message']='导入成功';
				$this->ajaxReturn($return);
			}else{
				$return['status']=0;
				$return['message']=$res['message'];
				$this->ajaxReturn($return);
			}
		}else
		{
			$return['status']=0;
			$return['message']='请选择上传的文件';
			$this->ajaxReturn($return);
		}
	}
	//导入产品
	function doimportGoods(){
		$g=session('company');
        $db_name=$g['db'];
		$trans=M('', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		$trans->startTrans();   // 开启事务
		$m=M('ArrivalOrder',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		$g=M('Goods', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		if (!empty($_FILES)){
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',    
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息 
				$return['status']=0;
				$return['message']=$upload->getError();
				$this->ajaxReturn($return);    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			
			for($i=2;$i<=$highestRow;$i++)
			{
				$data['goods_name']		= (string)$objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$data['bar_code']		= (string)$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['goods_no']=$data['bar_code'];//商品编码=条形码
				$data['goods_brand']		= (string)$objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['goods_marque']		= (string)$objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['goods_spec']		= (string)$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['price']		= (string)$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['receive_amount']		= (string)$objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data['net_weight']		= (string)$objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data['gross_weight']		= (string)$objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data['mark1']		= (string)$objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$data['mark2']		= (string)$objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				$data['storage_amount']=$data['receive_amount'];//库存
				$data['add_time']=time();
				$data['status']=1;
				$data['update_time']=time();
				
				//重复判断
				$map['goods_no']=$data['goods_no'];
				//添加
				$res=$m->add($data);	
				// echo $m->getlastsql();
				if($res){
					$g_info=$g->where($map)->find();//更新库存
					if(empty($g_info)){//没商品信息则新增商品信息
						$data_goods['bar_code']=$data['bar_code'];
						$data_goods['goods_no']=$data['goods_no'];
						$data_goods['goods_name']=$data['goods_name'];
						$data_goods['goods_brand']=$data['goods_brand'];
						$data_goods['goods_marque']=$data['goods_marque'];
						$data_goods['goods_spec']=$data['goods_spec'];
						// $data_goods['goods_pic']=$data['goods_pic'];
						$data_goods['goods_price']=$data['price'];
						$data_goods['storage_amount']=$data['receive_amount'];
						$data_goods['net_weight']=$data['net_weight'];
						$data_goods['gross_weight']=$data['gross_weight'];
						$res_goods=$g->add($data_goods);
						if(!$res_goods){
							$return['message']='添加库存信息失败';
							$return['status']=false;
							$this->ajaxReturn($return);
						}
					}else{//有商品信息则更新库存
						$order=A('Order');
						$order->edit_storagegoodsnums($map['goods_no'],$data['receive_amount']);
					}
				}else{
					$trans->rollback();
				}
			}
			$trans->commit();
			$return['status']=1;
			$return['message']='导入成功！';
			$this->ajaxReturn($return);
		}else
		{
			$return['status']=0;
			$return['message']='请选择上传的文件';
			$this->ajaxReturn($return);
		}
	}
}